package com.warmlight.voicepacket.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.FragmentActivity;
import android.text.SpannableString;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.github.siyamed.shapeimageview.RoundedImageView;
import com.warmlight.voicepacket.PlayDeatalActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.base.MBaseAdapter;
import com.warmlight.voicepacket.data.PlaydeatalData;
import com.warmlight.voicepacket.data.SearchResult;
import com.warmlight.voicepacket.data.eventbus.ShareUnlock;
import com.warmlight.voicepacket.db.VoicePacketDBHelper;
import com.warmlight.voicepacket.fragment.CustomBottomSheetDialogFragment;
import com.warmlight.voicepacket.http.HttpClient;
import com.warmlight.voicepacket.http.HttpConfig;
import com.warmlight.voicepacket.utils.JSONUtils;
import com.warmlight.voicepacket.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import okhttp3.Call;
/**
 * 搜索 头部 播单及文字描述adapter
 */
public class SearchHeaderAdapter extends MBaseAdapter<SearchResult.DataBean.CoverListBean> {

    private String mSearchkey = "";
    private final RequestOptions options;

    public SearchHeaderAdapter(Context context, List list) {
        super(context, list);
        options = new RequestOptions();
        options.placeholder(R.drawable.icon_roud_placeholder);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.view_search_cover,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.iv_search_cover = (RoundedImageView) convertView.findViewById(R.id.iv_search_cover);
            viewHolder.tv_search_cover_title = (TextView) convertView.findViewById(R.id.tv_search_cover_title);
            viewHolder.tv_search_cover_count = (TextView) convertView.findViewById(R.id.tv_search_cover_count);
            viewHolder.iv_lock_share = (ImageView) convertView.findViewById(R.id.iv_lock_share);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }

        final SearchResult.DataBean.CoverListBean item = getItem(position);
        String pic = item.getPic();
        final String id = item.getId();
        String name = item.getName();//播单名称
        String audio_num = item.getAudio_num();//播单曲数
        final String is_share = item.getIs_share();
        final String is_unlocked = item.getIs_unlocked();

        if ("1".equals(is_share) && "0".equals(is_unlocked)){
            //需要解锁
            viewHolder.iv_lock_share.setVisibility(View.VISIBLE);
        }else{
            //已解锁 或者无锁
            viewHolder.iv_lock_share.setVisibility(View.GONE);
        }
        if (options != null) {
            Glide.with(mContext).asBitmap().load(pic).apply(options).into(viewHolder.iv_search_cover);
        }
        viewHolder.tv_search_cover_count.setText(audio_num+"首");
        SpannableString spannableString = StringUtils.changeTextColor(Color.parseColor("#7b2aff"), name, mSearchkey);
        viewHolder.tv_search_cover_title.setText(spannableString);

        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //需要解锁
                if ("1".equals(is_share) && "0".equals(is_unlocked)){
                    //如果有锁就去跳转分享
                    SearchResult.DataBean.CoverListBean.ShareBean share = item.getShare();
                    PlaydeatalData.DataBean.ShareBean shareBean = new PlaydeatalData.DataBean.ShareBean();
                    shareBean.setDesc(share.getDesc());
                    shareBean.setH5_url(share.getH5_url());
                    shareBean.setPic_url(share.getPic_url());
                    shareBean.setTitle(share.getTitle());
                    new CustomBottomSheetDialogFragment(shareBean, new CustomBottomSheetDialogFragment.ShareCallBack() {
                        @Override
                        public void shareSuccess(boolean isSuccess) {
                            shareResult(isSuccess,item.getId(),position);
                        }
                    },true).show(((FragmentActivity)mContext).getSupportFragmentManager(), "search");
                }else {

                    Intent intentDetail = new Intent(mContext, PlayDeatalActivity.class);
                    intentDetail.putExtra(VoicePacketDBHelper.LIST_ID, id);
                    intentDetail.putExtra("type","search");
                    intentDetail.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    mContext.startActivity(intentDetail);
                }
            }
        });

        return convertView;
    }

    public void setSearchKey(String mSearchKey) {
        this.mSearchkey = mSearchKey;
    }

    public void updata(boolean islist, int item, String cover_id){
        for (int i=0;i<getCount();i++){
            SearchResult.DataBean.CoverListBean item1 = getItem(i);
            if (item1.getId().equals(cover_id)){
                item1.setIs_unlocked("1");
            }
        }
        notifyDataSetChanged();
    }

    private void shareResult(boolean isSuccess, final String id, final int position){
        if (isSuccess) {
            TreeMap<String,String> map = new TreeMap<>();
            map.put("audio_id",id);
            map.put("type","2");
            HttpClient.post(HttpConfig.SERVER + HttpConfig.SHARE, map, new com.warmlight.voicepacket.http.Callback() {
                @Override
                public void onFailure(Call call, IOException e) {

                }

                @Override
                public void onResponse(Call call, String response) {
//                    String string = response.body().string();
                    ArrayList<Map<String, String>> listMapByJson = JSONUtils.getListMapByJson(response);
                    if (listMapByJson.size()>0){
                        Map<String, String> map1 = listMapByJson.get(0);
                        if ("200".equals(map1.get("code"))){
                            EventBus.getDefault().post(new ShareUnlock(false,position,id));
//                            mList.get(position).setUnlockSuccess(true);
                        }
                    }
                }
            });
        }
    }

    static class ViewHolder {
        RoundedImageView iv_search_cover;
        TextView tv_search_cover_title;
        TextView tv_search_cover_count;
        ImageView iv_lock_share;
    }
}
